package cn.xfdzx.android.apps.shop.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ShopInGoodListAdapter;
import cn.xfdzx.android.apps.shop.adapter.ShopInOrderListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.GoodsUpStateBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopOrderListBeen;
import cn.xfdzx.android.apps.shop.bean.UpdateNumPriceBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ShopInGoodListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderListContract;
import cn.xfdzx.android.apps.shop.evenbus.SearchGoodsContentMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateShopOrderMessage;
import cn.xfdzx.android.apps.shop.presenter.ShopInGoodListPresenter;
import cn.xfdzx.android.apps.shop.presenter.ShopInOrderListPresenter;
import cn.xfdzx.android.apps.shop.util.KeybordUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：商家入驻 订单管理
 */
public class ShopInOrderFragment extends BaseMvpFragment<ShopInOrderListPresenter> implements ShopInOrderListContract.View ,ItemClickListener {

    RecyclerView rvAllOrder;
    SmartRefreshLayout mRefreshLayout;


    private ShopInOrderListAdapter orderListAdapter;
    private int page = 1;
    private int limit = Constants.LIMIT;
    private String orderType, styleType;
    private List<ShopOrderListBeen.DataBean> mList = new ArrayList();
    private Dialog dialog;
    private Dialog ziti_dialog;
    private int now_position;
    private EditText ettGoodPrice, ettGoodYouhuiPrice, ettKucun;
    private TextView tvDialogQueren;
    private LinearLayout llHint;
    private String keyWord="";
    private  View ziti_layout;

    @Override
    protected void initView(View view) {

        rvAllOrder=view.findViewById(R.id.rv_all_order);
        mRefreshLayout=view.findViewById(R.id.refreshLayout);
        llHint=view.findViewById(R.id.ll_hint);
        EventBus.getDefault().register(this);

        orderType = getArguments().getString("orderType");
        styleType = getArguments().getString("styleType");
        mPresenter = new ShopInOrderListPresenter();
        mPresenter.attachView(this);


        ziti_dialog = new Dialog(getActivity(), R.style.ExitDialog);
          ziti_layout = View.inflate(getActivity(), R.layout.dialog, null);
        ziti_dialog.setContentView(ziti_layout);
        ziti_dialog.setCanceledOnTouchOutside(true);
        ((TextView) ziti_layout.findViewById(R.id.tv_content)).setText("确认客户已提货？");
        ziti_layout.findViewById(R.id.tvw_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ziti_dialog.dismiss();
            }
        });


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvAllOrder.setLayoutManager(linearLayoutManager);
        rvAllOrder.setItemAnimator(new DefaultItemAnimator());
        orderListAdapter = new ShopInOrderListAdapter(getContext(), mList, styleType,this);

        rvAllOrder.setAdapter(orderListAdapter);


        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.initData(true, false, orderType,keyWord, page + "", limit + "");
            }
        });
        dialog = new Dialog(getActivity(), R.style.ExitDialog);
        View layout = View.inflate(getActivity(), R.layout.dialog_good_bianji, null);
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(true);
        layout.findViewById(R.id.tvw_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        ettGoodPrice = layout.findViewById(R.id.ett_good_price);
        ettGoodYouhuiPrice = layout.findViewById(R.id.ett_good_youhui_price);
        ettKucun = layout.findViewById(R.id.ett_kucun);
        tvDialogQueren = layout.findViewById(R.id.tv_dialog_queren);
        if (styleType.equals("0")) {
            tvDialogQueren.setTextColor(getResources().getColor(R.color.color_FB9129));
        } else {
            tvDialogQueren.setTextColor(getResources().getColor(R.color.color_02C557));
        }



    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_all_order;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }
    private String nowString;
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onSearchEvent(SearchGoodsContentMessage messageEvent) {
        KeybordUtil.closeKeybord(getActivity());
        keyWord = messageEvent.getSearchMessage();
        if (messageEvent.getPage()==0){
            nowString="all";
        }else if (messageEvent.getPage()==1){
            nowString="0";
        }else if (messageEvent.getPage()==2){
            nowString="1";
        }else if (messageEvent.getPage()==3){
            nowString="2";
        }else if (messageEvent.getPage()==4){
            nowString="3";
        }
        if (orderType.equals(nowString)){
            page = 1;
            mPresenter.initData(true, true, orderType, keyWord,page + "", limit + "");
        }

    }
    @Override
    public void onOrderListSuccess(ShopOrderListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initData(false, false, orderType,keyWord, page + "", limit + "");
                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
            mList.addAll(bean.getData());
            orderListAdapter.notifyDataSetChanged();
            if (mList.size()==0){
                llHint.setVisibility(View.VISIBLE);
            }else {
                llHint.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onConfirmSuccess(ConfirmBeen bean, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.remove(position);
            orderListAdapter.notifyDataSetChanged();
        }  else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }




    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(UpdateShopOrderMessage messageEvent) {
        if (messageEvent.getMessage().equals(orderType)) {
            page = 1;
            mPresenter.initData(true, true, orderType,keyWord, page + "", limit + "");

        }
    }

    public static ShopInOrderFragment newInstance(String orderType, String styleType) {
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType + "");
        bundle.putString("styleType", styleType + "");
        ShopInOrderFragment fragment = new ShopInOrderFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        if (dialog.isShowing()){
            dialog.dismiss();
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        mList.clear();
        mList = null;
        super.onDestroy();
    }


    @Override
    public void onItemClick(int position) {
        ziti_layout.findViewById(R.id.tvw_dialog_determine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ziti_dialog.dismiss();
                mPresenter.confirm(mList.get(position).getOrder_id(),position);
            }
        });
        ziti_dialog.show();
    }
}
